import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/11 10:09
 * @description:
 */
public class UserDaoIO implements UserDao {

  @Override
  public void register(User user)  {
    BufferedWriter bw=null;
    try {
      bw=new BufferedWriter(new FileWriter("d:\\a.txt",true ));
      bw.write(user.getId()+"--"+user.getName()+"--"+user.getPassword()+"--"+user.getAge());
      bw.newLine();
      bw.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      if (bw!=null){
        try {
          bw.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

  }

  @Override
  public boolean login(String name, String password) {
    BufferedReader br=null;
    try {
      br=new BufferedReader(new FileReader("d:\\a.txt"));
    String line=null;
      while ((line=br.readLine())!=null){
        String []data=line.split("--");
        if (data[1].equals(name)&&data[2].equals(password)){
          return true;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      if (br!=null){
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        finally {
          try {
            br.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    return false;
  }

  @Override
  public void quiteAll() {
BufferedReader br=null;
    try {
      br=new BufferedReader(new FileReader("d:\\a.txt"));
    String line=null;
      while ((line=br.readLine())!=null){
        System.out.println(line);
      }
     } catch (Exception e) {
      e.printStackTrace();
    }finally {
      if (br!=null){
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
